package osutil;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;

public class SerBuffImage implements Serializable {
	BufferedImage image;
	
	public BufferedImage getImage() {
		return image;
	} 

	public void setImage(BufferedImage image) {
		this.image = image;
	}


	private void writeObject(java.io.ObjectOutputStream out) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(image, "JPG", baos);
		baos.flush();
		
		byte[] buf = baos.toByteArray();		
		out.writeInt(buf.length);		
		out.write(buf);
		baos.close();
	}

	private void readObject(java.io.ObjectInputStream in) throws IOException,
			ClassNotFoundException {
		int length = in.readInt();
		byte[] img = new byte[length];
		in.readFully(img);
		image = ImageIO.read(new ByteArrayInputStream(img));
	}

	public SerBuffImage(BufferedImage image) {
		super();
		this.image = image;
	}
}
